﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using umbraco.cms.businesslogic.datatype;
using umbraco.interfaces;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
using Core.Module;
using umbraco.cms.presentation.Trees;
using Module.MultiSiteFields.Interfaces;
using Module.MultiSiteFields.Controllers;
using System.Xml.Linq;

namespace Module.MultiSiteFields.Controls.DynamicTreePicker
{
    public class DynamicTreePicker : AbstractDataEditor
    {
        public DynamicTreePicker()
        {
            DBType = DBTypes.Ntext;
        }
        public override Guid Id
        {
            get
            {
                return new Guid("9518B610-C8D1-46CA-8865-B180EF5E14DE");
            }
        }
        public override string DataTypeName
        {
            get
            {
                return "_MultiSite Content Data Picker";
            }
        }
        private IDataPrevalue prevalueEditor;
        public override IDataPrevalue PrevalueEditor
        {
            get
            {
                if (prevalueEditor == null) prevalueEditor = new PropertyPrevaluesEditor(this);
                return prevalueEditor;
            }
        }

        private IDataEditor dataEditor;
        public override IDataEditor DataEditor
        {
            get
            {
                if (dataEditor == null) dataEditor = new PropertyDataEditor(this);
                return dataEditor;
            }
        }

        private IData _Data;
        public override IData Data
        {
            get
            {

                if (_Data == null)
                {
                    _Data = new DynamicTreeData(this)
                    {
                        Value = (new DynamicTreeValue() { Nodes = new DynamicTreeValue.Node[] { } }).ToXml()
                    };                    
                }
                return _Data;
            }
        }



    }

    public class DynamicTreeData : DefaultData
    {
        public DynamicTreeData(BaseDataType DataType) : base(DataType) { }

        public override XmlNode ToXMl(XmlDocument data)
        {
            string str = (this.Value != null) ? this.Value.ToString() : string.Empty;
            if (string.IsNullOrEmpty(str)) return base.ToXMl(data);
            XmlElement tr=data.CreateElement("data");
            tr.InnerXml=str;            
            return tr.FirstChild;
        }

    }

    [Serializable]
    [XmlRoot("nodes")]
    public class DynamicTreeValue //: IXmlSerializable
    {
        [XmlRoot("node")]
        public class Node
        {
            [XmlAttribute("id")] 
            public string id { get; set; }
            [XmlAttribute("type")] 
            public string type { get; set; }
            [XmlText]
            public string data { get; set; }
        }
        [XmlElement("node")]
        public Node[] Nodes { get; set; }


        //public XmlSchema GetSchema()
        //{
        //    return null;
        //}

        //public void ReadXml(XmlReader reader)
        //{
        //    List<Node> nodes = new List<Node>();
        //    if (reader.Read() && reader.ReadToFollowing("node"))
        //    {
        //        while (reader.Name.Equals("node"))
        //        {
        //            Node nd = new Node();
        //            nd.id = reader.GetAttribute("id");
        //            nd.type = reader.GetAttribute("type");
        //            nd.data = reader.ReadElementContentAsString();
        //            nodes.Add(nd);
        //        }
        //    }
        //    Nodes = nodes.ToArray();
        //}
        //public void WriteXml(XmlWriter writer)
        //{
        //    writer.WriteStartElement("nodes");
        //    if (Nodes != null)
        //        foreach (Node nitem in Nodes)
        //        {
        //            writer.WriteStartElement("node");
        //            writer.WriteAttributeString("id", nitem.id);
        //            writer.WriteAttributeString("type", nitem.type);
        //            writer.WriteRaw(nitem.data);
        //            writer.WriteEndElement();
        //        }
        //    writer.WriteEndElement();
        //}


    }

    [Serializable]
    public class DynamicTreePreValue
    {
        public bool HideLabel { get; set; }
        public bool IsSinlgePicker { get; set; }
        public Int32 MaxAmountItems { get; set; }
        public Int32 MinAmountItems { get; set; }
        public TreeStartPoint[] Types { get; set; }
    }
}
